//
//  XHFeedbackViewController.m
//  xinhua_sx
//
//  Created by 党阳阳 on 14-7-16.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

#import "XHFeedbackViewController.h"
#import "AFNetworking.h"
#import "MBProgressHUD.h"

@implementation XHFeedbackViewController
@synthesize textv = _textv;
-(void)viewDidLoad
{
    CGFloat aY = kIOS_VERSION<7.0?0:20;
     UILabel* _titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, aY+50, self.view.frame.size.width, 44)];
    _titleLable.backgroundColor = [UIColor clearColor];
    _titleLable.textColor = [UIColor blackColor];
    _titleLable.font = [UIFont systemFontOfSize:20];
    _titleLable.textAlignment = NSTextAlignmentCenter;
    _titleLable.text = @"意见反馈";
    [self.view addSubview:_titleLable];
    UITextView* tex = [[UITextView alloc] initWithFrame:CGRectMake(10, aY+90, 300, 120)];
    tex.layer.borderWidth = 1.0;
    tex.layer.borderColor = [XHFeedbackViewController colorWithHexString:@"#A6C4E8"].CGColor;
    [self.view addSubview:tex];
    
    self.textv = tex;
    UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(10, aY+220, 300, 30)];
    [button setTitle:@"提交" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(tijiao) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundColor:[XHFeedbackViewController colorWithHexString:@"#A6C4E8"]];
    [self.view addSubview:button];
    
  
}
-(void)tijiao
{
    [_textv resignFirstResponder];
    if ([_textv.text length] == 0)
    {
        [_textv becomeFirstResponder];
        return;
    }
    NSLog(@"223");
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    NSString* str ;

    str = [NSString stringWithFormat:@"http://183.203.18.45:7001/xinhua/m/feedback"];
    NSString* uid = [[NSUserDefaults standardUserDefaults] objectForKey:@"UID"];
    if (!uid)
    {
        UIAlertView* viw = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有登录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [viw show];
        return;
    }
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    HUD.labelText =@"正在提交";
    HUD.dimBackground = YES;
    [self.view addSubview:HUD];
    [HUD show:YES];
    /*NSString* sg = [NSString stringWithFormat:@"opinion=%@&versionName=1&frontUserId=%@&code=140725",_textv.text,uid];
    NSData* dag = [sg dataUsingEncoding:NSUTF8StringEncoding];
    
 
    NSLog(@"string = %@",sg);
    NSData *data = [sg dataUsingEncoding:NSUTF8StringEncoding];
    
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:str]];
    
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setTimeoutInterval:10];
    
    [urlRequest setHTTPBody:data];
    
    NSOperationQueue *queue= [[NSOperationQueue alloc] init];
    
    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        [HUD hide:YES];
        if ([data length]>0 && connectionError == nil) {
            
            NSMutableDictionary *dic =[NSMutableDictionary dictionaryWithDictionary: [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&connectionError]];
            NSLog(@"dic = %@",dic );
            NSLog(@"ff= %@",[dic objectForKey:@"result"]);
            if ([[dic objectForKey:@"result"] isEqualToString:@"success"]) {
                NSLog(@"222");
                UIAlertView* viw = [[UIAlertView alloc] initWithTitle:@"提示" message:@"意见反馈成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
              [viw show];
                
            }
          
            
            
        }else if (connectionError != nil){
            
           
            NSLog(@"%@",connectionError);
            
        }
        
        
    }];*/
   
    NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:_textv.text,@"oponion",COUTURYCODE,@"code",@"versionName",@"22",uid,@"frontUserId", nil]; //复制时替换成 县区code
    [manager POST:str parameters:dic success:^(AFHTTPRequestOperation*operation, id responseObject) {
        [HUD hide:YES];
        id jsonObject = [NSJSONSerialization JSONObjectWithData:operation.responseData
                                                        options:NSJSONReadingAllowFragments
                                                          error:nil];
        NSLog(@"json11 = %@",[jsonObject objectForKey:@"message"]);
        UIAlertView* viw = [[UIAlertView alloc] initWithTitle:@"提示" message:[jsonObject objectForKey:@"message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [viw show];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        [HUD hide: YES];
        NSLog(@"error = %@",error);
        UIAlertView* viw = [[UIAlertView alloc] initWithTitle:@"提示" message:@"意见反馈失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [viw show];
    }];
    butoncl* al = [[butoncl alloc] init];
    [al postString:@"021"fff:@""];
   // [HUD hide:YES];

   

}
-(void)shouW
{
     UIAlertView* viw = [[UIAlertView alloc] initWithTitle:@"提示" message:@"意见反馈成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [viw show];
}
+ (UIColor *) colorWithHexString: (NSString *)color
{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //r
    NSString *rString = [cString substringWithRange:range];
    
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}

@end
